package com.util;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * @description 封装时间方法
 * @author LIUSHUGE
 * @since 2013-1-23
 * @version 1.0
 */
public class DateUtil {
	public static Date date = new Date();
	private static SimpleDateFormat sf = new SimpleDateFormat(
			"yyyy-MM-dd 00:00:00");
	private static SimpleDateFormat sf1 = new SimpleDateFormat(
			"yyyy-MM-dd 23:59:59");
	private static SimpleDateFormat sf2 = new SimpleDateFormat("yyyy-MM-dd");
	private static SimpleDateFormat sf4 = new SimpleDateFormat(
			"yyyy-MM-dd HH:mm:ss");
	private static SimpleDateFormat sf5 = new SimpleDateFormat("yyyy-MM");
	private static SimpleDateFormat sf6 = new SimpleDateFormat(
			"yyyy-MM-dd 18:00:00");
	private static SimpleDateFormat sf7 = new SimpleDateFormat("yyyy.MM.dd");
	private static SimpleDateFormat sf8 = new SimpleDateFormat("yyyy年MM月dd日");
	private static SimpleDateFormat sf9 = new SimpleDateFormat("yyyyMMdd");
	private static SimpleDateFormat sf10 = new SimpleDateFormat("yyyyMM");
	private static SimpleDateFormat sf11 = new SimpleDateFormat("yyyy");
	private static SimpleDateFormat sf12 = new SimpleDateFormat(
			"yyyyMMddHHmmss");
	private static SimpleDateFormat sf13 = new SimpleDateFormat(
			"EEE MMM dd HH:mm:ss zzz yyyy", Locale.ENGLISH);
	private static SimpleDateFormat sf14 = new SimpleDateFormat("dd");
	private static SimpleDateFormat sf16 = new SimpleDateFormat("HH:mm:ss");
	private static SimpleDateFormat sf17 = new SimpleDateFormat(
			"yyyy-MM-01 00:00:00");
	private static SimpleDateFormat sf18 = new SimpleDateFormat(
			"yyyy-MM-31 23:59:59");

	public static String getStart() {
		String str = sf.format(date);
		return str;
	}

	public static String getEnd() {
		String str1 = sf1.format(date);
		return str1;
	}

	public static String getYesEtStart() {
		long time = date.getTime() - 86400000;
		Date yes = new Date(time);
		String s = sf.format(yes);
		return s;
	}

	public static String getYesEtEnd() {
		long time = date.getTime() - 86400000;
		Date yes = new Date(time);
		String s = sf1.format(yes);
		return s;
	}

	public static String get(Date date) {
		return sf2.format(date);
	}

	public static long getModDate(int day) {
		String d = "";
		if (day > 9) {
			StringBuffer sb = new StringBuffer();
			sb.append(day);
			d = sb.toString();
		} else {
			StringBuffer sb = new StringBuffer();
			sb.append(day);
			d = 0 + sb.toString();
		}
		SimpleDateFormat sf3 = new SimpleDateFormat("yyyy-MM-" + d
				+ " 23:59:59");
		String str = sf3.format(date);
		Date date = null;
		try {
			date = sf4.parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date.getTime();
	}

	public static long getFirstDay(long l) {
		Date date = new Date(l);
		String str = sf.format(date);
		Date date0 = null;
		try {
			date0 = sf4.parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date0.getTime();
	}

	public static String getBeforeMonth(Date date) {
		return sf5.format(date);
	}

	public static Date getJzTime(Date dd) {
		String str = sf6.format(dd);
		Date date = null;
		try {
			date = sf4.parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}

	public static Date getDate(String str) {
		try {
			return sf2.parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}

	}
    public static String getDate(Date date) {
		
		return sf2.format(date);
	}
	public static Date getDTime(Date currdate) {
		String string = sf4.format(currdate);
		try {
			date = sf4.parse(string);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}

	public static String getDaily(long time) {
		Date date = new Date(time);
		String str = sf7.format(date);
		return str;
	}

	public static String getYMD(long time) {
		Date date = new Date(time);
		String str = sf8.format(date);
		return str;
	}

	public static Date getS(String str) {
		StringBuffer sb = new StringBuffer(str);
		sb.append("01");
		Date date = new Date();
		try {
			date = sf9.parse(sb.toString());
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}

	public static String getYear(Date date) {
		return sf11.format(date);
	}

	public static String getMonth(Date date) {
		return sf10.format(date);
	}

	public static String getDay(Date date) {
		return sf14.format(date);
	}

	public static Date getDate(long l) {
		Date date = new Date(l);
		String str = sf2.format(date);
		Date d = new Date();
		try {
			d = sf2.parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return d;
	}

	public static Date getDateByStr(String str) {
		Date date = new Date();
		try {
			date = sf10.parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}

	public static Date getDateByStr1(String str) {
		Date date = new Date();
		try {
			date = sf4.parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}

	public static String getFullStr(Date date) {
		return sf12.format(date);
	}

	public static String getStrByDate(Date date) {
		return sf4.format(date);
	}

	public static Date getExcelDate(String str) {
		Date date = new Date();
		try {
			date = sf13.parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}

	public static Date getCurrDate(Date date) {
		String currStr = sf.format(date);
		Date currDate = null;
		try {
			currDate = sf4.parse(currStr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return currDate;
	}

	public static String getYMD(Date date) {
		return sf9.format(date);
	}

	public static java.sql.Date getConverDate(Object dateObj) {
		java.sql.Date date = null;
		if (dateObj == null || !(dateObj instanceof Timestamp)) {
			return date;
		} else {
			if (((Timestamp) dateObj).getTime() == 0)
				return null;
			date = new java.sql.Date(((Timestamp) dateObj).getTime());
		}
		return date;
	}

	public static Date getCurrTime(Date date) {
		Date d = null;
		String string = sf16.format(date);
		try {
			d = sf16.parse(string);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return d;
	}

	public static Date getTime(String str) {
		try {
			return sf16.parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
	}

	public static Date getMStart(Date date) {
		String str = sf17.format(date);
		Date d = null;
		try {
			d = sf4.parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return d;
	}

	public static Date getMEnd(Date date) {
		String str = sf18.format(date);
		Date d = null;
		try {
			d = sf4.parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return d;
	}

	public static Date getDStart(Date date) {
		String str = sf.format(date);
		Date d = null;
		try {
			d = sf4.parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return d;
	}

	public static Date getDEnd(Date date) {
		String str = sf1.format(date);
		Date d = null;
		try {
			d = sf4.parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return d;
	}

	/**
	 * 获得当前时间的月的第一天和最后一天
	 */
	public static Date[] getFirstAndEnd(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		Date[] dates = new Date[2];
		Date firstDateOfMonth, lastDateOfMonth;
		// 得到当天是这月的第几天
		int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
		// 减去dayOfMonth,得到第一天的日期，因为Calendar用0代表每月的第一天，所以要减一
		calendar.add(Calendar.DAY_OF_MONTH, -(dayOfMonth - 1));
		firstDateOfMonth = calendar.getTime();
		// calendar.getActualMaximum(Calendar.DAY_OF_MONTH)得到这个月有几天
		calendar.add(Calendar.DAY_OF_MONTH, calendar
				.getActualMaximum(Calendar.DAY_OF_MONTH) - 1);
		lastDateOfMonth = calendar.getTime();
		dates[0] = firstDateOfMonth;
		dates[1] = lastDateOfMonth;
		return dates;
	}

	/**
	 * 得到开始日期到结束日期内的所有日期
	 * 
	 * @param start
	 * @param end
	 * @param calendarType 类型
	 * @return
	 */
	public static Date[] getDateArrays(Date start, Date end, int calendarType) {
		ArrayList<Date> ret = new ArrayList<Date>();
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(start);
		Date tmpDate = calendar.getTime();
		while (tmpDate.before(end)) {
			ret.add(calendar.getTime());
			calendar.add(calendarType, 1);
			tmpDate = calendar.getTime();
		}
		Date[] dates = new Date[ret.size()];
		return (Date[]) ret.toArray(dates);
	}
	/**
	 * String类型转换成Date
	 * @param str
	 * @return
	 */
	public static Date getDateFromString(String str){
		SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd");
		Date d = null;
		try {
			d = sim.parse(str);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return d;
	}
	
}
